package org.wattdepot.cli;

import java.util.ArrayList;
import java.util.List;
import org.wattdepot.client.WattDepotClientException;
import org.wattdepot.resource.source.jaxb.Source;
import org.wattdepot.resource.property.jaxb.Property;

/**
 * Lists information about what type of fuel a power plant uses.
 * Input 1:  fueltypes SIM_IPP
 * Input 2:  fueltypes SIM_KAHE
 * @author Edwin Lim, Wing Ma Cheung
 * @version 2.0
 *
 */
public class FuelTypes extends CommandLineInterface implements WattDepotCliCommand {
  /**
   * Calls the method that would print the summarized list of a single power plant.
   * @param args Strings of input arguments
   * @return output a collection of information about one source
   */
  @Override
  public String doCommand(List<String> args) {
    String input = args.get(0);
    String output = "";
    Source source = null;
    // array list that will hold the subsources as a string
    List<String> subsources = new ArrayList<String>();
    // Check if the input source is valid
    try {
      source = searchSource(input);
    }
    catch (WattDepotClientException eCli) {
      eCli.printStackTrace();
    }
    
    if (source == null) {
      return sourceNotFound;
    }
    
    // Creates string buffers for all the outputs 
    StringBuffer lsfo = new StringBuffer(255);
    StringBuffer waste = new StringBuffer(255);
    StringBuffer coal = new StringBuffer(255);
    StringBuffer virtualSource = new StringBuffer(255);
    StringBuffer subOutput = new StringBuffer(255);
    //check if the source is virtual 
    if (source.isVirtual()) {
      // check if any subsource is available
      if (source.isSetSubSources()) {       
        for (String subsource : source.getSubSources().getHref()) {
          // finds the substring that starts with SIM
          int i = subsource.indexOf("SIM");
          subsources.add(subsource.substring(i));
        }    
      }   
      // Convert each string source into a source class
      for (String test : subsources) {
        try {
          source = searchSource(test);
        }
        catch (WattDepotClientException eCli) {
          eCli.printStackTrace();
        }
        // print fueltypes
        if (source.isSetProperties()) {
          for (Property property : source.getProperties().getProperty()) {
            if (property.getValue().contains("LSFO")) {
              lsfo.append(source.getName()).append(", ");
            }
            else if (property.getValue().equals("waste")) {
              waste.append(source.getName()).append(", ");
            }
            else if (property.getValue().equals("coal")) {
              coal.append(source.getName()).append(", ");
            }
          }
        }
      }
      subOutput.append("LSFO: ").append(lsfo);
      subOutput.append(cr);
      subOutput.append("waste: ").append(waste);
      subOutput.append(cr);
      subOutput.append("coal: ").append(coal);
      subOutput.append(cr);
      output = subOutput.toString();
    }
    // check if the source is non virtual
    else {
      virtualSource.append(source.getProperty("fuelType"));
      String bunchString = ": " + source.getName();
      virtualSource.append(bunchString);
      output = virtualSource.toString();
    }
    return output;
  }
}

